
_realname=qgis
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=3.32.1
pkgrel=3
pkgdesc='Geographic Information System (GIS) that supports vector, raster & database formats (mingw-w64)'
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url='https://qgis.org/'
license=('spdx:GPL-2.0-or-later')
depends=("${MINGW_PACKAGE_PREFIX}-protobuf"
         "${MINGW_PACKAGE_PREFIX}-gdal"
         "${MINGW_PACKAGE_PREFIX}-libzip"
         "${MINGW_PACKAGE_PREFIX}-qca-qt5"
         "${MINGW_PACKAGE_PREFIX}-gsl"
         "${MINGW_PACKAGE_PREFIX}-exiv2"
         "${MINGW_PACKAGE_PREFIX}-hdf5"
         "${MINGW_PACKAGE_PREFIX}-libspatialite"
         "${MINGW_PACKAGE_PREFIX}-libxml2"
         "${MINGW_PACKAGE_PREFIX}-netcdf"
         "${MINGW_PACKAGE_PREFIX}-opencl-icd"
         #"${MINGW_PACKAGE_PREFIX}-pdal"
         "${MINGW_PACKAGE_PREFIX}-proj"
         "${MINGW_PACKAGE_PREFIX}-python-gdal"
         "${MINGW_PACKAGE_PREFIX}-python-owslib"
         "${MINGW_PACKAGE_PREFIX}-python-pyqt5"
         "${MINGW_PACKAGE_PREFIX}-python-qscintilla-qt5"
         "${MINGW_PACKAGE_PREFIX}-qt5-3d"
         "${MINGW_PACKAGE_PREFIX}-qt5-base"
         "${MINGW_PACKAGE_PREFIX}-qt5-declarative"
         "${MINGW_PACKAGE_PREFIX}-qt5-gamepad"
         "${MINGW_PACKAGE_PREFIX}-qt5-location"
         "${MINGW_PACKAGE_PREFIX}-qt5-multimedia"
         "${MINGW_PACKAGE_PREFIX}-qt5-serialport"
         "${MINGW_PACKAGE_PREFIX}-qt5-svg"
         "${MINGW_PACKAGE_PREFIX}-qtkeychain-qt5"
         $([[ ${CARCH} == aarch64 ]] || echo "${MINGW_PACKAGE_PREFIX}-qtwebkit")
         "${MINGW_PACKAGE_PREFIX}-qscintilla-qt5"
         "${MINGW_PACKAGE_PREFIX}-qwt-qt5"
         "${MINGW_PACKAGE_PREFIX}-spatialindex")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-opencl-clhpp"
             "${MINGW_PACKAGE_PREFIX}-pyqt-builder"
             "${MINGW_PACKAGE_PREFIX}-python"
             "${MINGW_PACKAGE_PREFIX}-qt5-tools")
source=("https://qgis.org/downloads/${_realname}-$pkgver.tar.bz2"
        "001-fix-building-with-mingw-w64-clang.patch"
        "002-fix-settingstree-init-order-clang.patch"
        "003-fix-pyrcc-command.patch"
        "004-fix-customwidgets-install.patch"
        "005-fix-build-with-protobuf-23.patch")
sha256sums=('6a31abd2869f0850724d88c164703cf161c83893adc663a32e68da685f48b7b2'
            '8a1c67b3bed1545813044f842cc0f6ff1076849e27379db6f64092014936b4c5'
            'e880eef26fa06d8fae291346f87735e140145c2d65558fce69e480e303c629e2'
            'c0f599996d90da55d1370f7144d545352d0075ca7876d4ade52c520b237ce318'
            '216dc67ef707f2e918afb7d190c9ea75c11065dea0d23d11b94cab5d8463bac3'
            'ac6c96e88346c1cec739b1e628afb02aef1895c0d09213269bad75b1a8cee617')

prepare() {
  cd "${srcdir}"/${_realname}-${pkgver}
  if [[ ${MINGW_PACKAGE_PREFIX} == *-clang-* ]]; then
    patch -p1 -i "${srcdir}"/001-fix-building-with-mingw-w64-clang.patch
    patch -p1 -i "${srcdir}"/002-fix-settingstree-init-order-clang.patch
  fi
  patch -p1 -i "${srcdir}"/003-fix-pyrcc-command.patch
  patch -p1 -i "${srcdir}"/004-fix-customwidgets-install.patch
  patch -p1 -i "${srcdir}"/005-fix-build-with-protobuf-23.patch
}

build() {
  mkdir -p ${srcdir}/build-${MSYSTEM} && cd ${srcdir}/build-${MSYSTEM}

  declare -a _extra_config
  if check_option "debug" "n"; then
    _extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    _extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  if [[ ${CARCH} == aarch64 ]]; then
    _extra_config+=("-DWITH_QTWEBKIT=OFF")
  fi

  CXXFLAGS+=" -DQWT_POLAR_VERSION=0x060200 -Wno-ignored-attributes"
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake \
    -G"Ninja" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DCMAKE_CXX_STANDARD=17 \
    "${_extra_config[@]}" \
    -DPython_EXECUTABLE=${MINGW_PREFIX}/bin/python \
    -DQGIS_BIN_SUBDIR=bin \
    -DQGIS_DATA_SUBDIR=share \
    -DQGIS_LIBEXEC_SUBDIR=bin \
    -DQGIS_PLUGIN_SUBDIR=lib/qgis/plugins \
    -DQT_PLUGINS_DIR=share/qt5/plugins \
    -DWITH_3D=ON \
    -DWITH_CUSTOM_WIDGETS=ON \
    -DWITH_QWTPOLAR=ON \
    -DQWTPOLAR_LIBRARY=${MINGW_PREFIX}/lib/libqwt-qt5.dll.a \
    -DQWTPOLAR_INCLUDE_DIR=${MINGW_PREFIX}/include/qwt-qt5 \
    -DWITH_INTERNAL_QWTPOLAR=OFF \
    -DENABLE_TESTS=OFF \
    -DWITH_BINDINGS=ON \
    -DWITH_GRASS=OFF \
    -DWITH_PDAL=OFF \
    ../${_realname}-$pkgver

  ${MINGW_PREFIX}/bin/cmake --build .
}

package() {
  cd "${srcdir}/build-${MSYSTEM}"
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake --install .
}
